/*
*JavaScript Video Tag version 1.0
*Copyright (c) 2008 Leonardo Rossetti motw.leo@gmail.com
*Written by Leonardo Rossetti motw.leo@gmail.com
*Project page and documentation: http://code.google.com/p/jsvideotag/
*This software is under MIT license: http://www.opensource.org/licenses/mit-license.php
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*THE SOFTWARE.
*/
(function () {
	var jsvideo = {};
	//properties
	jsvideo.body = document.getElementsByTagName("*") || document.all;
	jsvideo.list = [];
	jsvideo.plugins = {};
	jsvideo.plugins.support = function (activex, re) {
		if (window.ActiveXObject) {
			try {
				new ActiveXObject(activex);
				return true;
			} catch (e) {
				return false;
			}
		}
		for (var i = 0; i < navigator.plugins.length; i++) {
			if (re.exec(navigator.plugins[i].name)) {
				return true;
			}
		}
		return false;
	};
	jsvideo.plugins.ref = [
		{
			ext: "swf", 
			name: "flash", 
			mimetype: "application/x-shockwave-flash", 
			base: "http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab", 
			page: "http://www.adobe.com/go/getflashplayer", 
			has: jsvideo.plugins.support("ShockwaveFlash.ShockwaveFlash", /flash/gim), 
			classid: "clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" 
		}
	];
	//methods
	jsvideo.setList = function () {
		for (var i = 0; i < this.body.length; i++) {
			if (this.body[i].tagName.toLowerCase() === "span" && this.body[i].className === "jsvideotag") {
				this.list[this.list.length] = this.body[i];
			}
		}
	};

	jsvideo.check = function (name) {
		for (var i = 0; i < this.plugins.ref.length; i++) {
			if (this.plugins.ref[i].ext.toLowerCase().indexOf(name) != -1 && this.plugins.ref[i].has) {
				return this.plugins.ref[i];
			}
		}
		return null;
	};

	jsvideo.replaceHTML = function (e, r) {
		var emb = document.createElement("embed");
		var obj = document.createElement("object");
		var param;
		var params = [
			{
				name: "movie", 
				value: e.getAttribute("src")
			}, 
			{
				name: "quality", 
				value: e.getAttribute("quality") ? e.getAttribute("quality") : "high"
			}, 
			{
				name: "wmode", 
				value: e.getAttribute("wmode") ? e.getAttribute("wmode") : "transparent"
			}, 
			{
				name: "classid", 
				value: r.classid
			}
		];
		obj.data = e.getAttribute("src");
		obj.width = e.getAttribute("width");
		obj.height = e.getAttribute("height");
		emb.src = e.getAttribute("src");
		emb.width = e.getAttribute("width");
		emb.height = e.getAttribute("height");
		for (var p in r) {
			if (typeof r[p] != "function") {
				obj[p] = r[p];
			}
		}
		
		for (var i = 0; i < params.length; i++) {
			param = document.createElement("param");
			param.name = params[i].name;
			param.value = params[i].value;
			obj.appendChild(param);
		}
		
		e.parentNode.replaceChild(window.ActiveXObject ? emb : obj, e);
		

	};

	jsvideo.insert = function () {
		var ext = "";
		var current;
		for (var i = 0; i < this.list.length; i++) {
			ext = this.list[i].getAttribute("src").match(/\..*/gim)[0].replace(".","");
			current = this.check(ext);
			if (current) {
				this.replaceHTML(this.list[i], current);
			}
		}
	};
	
	jsvideo.init = function () {
		this.setList();
		this.insert();
	};

	//start on page load
	if (window.addEventListener) {
		window.addEventListener("load", function () {
			jsvideo.init();
		}, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", function () {
			jsvideo.init();
		});
	}
})();